package cn.hzy.admin.core.filter;

import cn.hzy.toolkit.ApiResult;
import cn.hzy.toolkit.MessageBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 异常 过滤器
 * </p>
 *
 * @author hzy
 * @since 2020-04-26
 */
@ControllerAdvice
public class FilterExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(FilterHandlerInterceptor.class);

    @ResponseBody
    @ExceptionHandler(value = MessageBox.class)
    public ApiResult messageBoxHandler(MessageBox e) {
        e.printStackTrace();
        return e.getApiResult();
    }

    @ExceptionHandler(value = Exception.class)
    public ApiResult exceptionHandler(Exception e) {
        var errMessage = e.getMessage();
        logger.error(errMessage, e);
        e.printStackTrace();
        return ApiResult.result(ApiResult.StatusCodeEnum.Error, errMessage);
    }


}
